<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: timeStamp.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: timeStamp.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>
/**
 * **将时间戳转换成自定义格式**
 * @param {Number} [time] -时间戳
 * @param {String} [type=all] -自定义输出格式，年、月、日、时、分、秒（n、y、r、s、f、m）；默认all输出所有；
 * @param {String} [separator=null] -自定义连接符
 * @returns -返回自定义日期
 * 
 * @example &lt;caption>年-月-日&lt;/caption>
 * timeStamp(1618298335,'nyr','-')
 * //return 2020-01-02
 * @example &lt;caption>年、月、日、时、分、秒可任意组合&lt;/caption>
 * timeStamp(1618298335,'nr','|')
 * //return 2020|02
 */




const timeStamp=(time,type='all',separator=null)=>{
    let date = new Date(time*1000),
    sep = null;

    let dateArr={
        n : date.getFullYear(),
        y : date.getMonth() + 1,
        r : date.getDate(),
        s : date.getHours(),
        f : date.getMinutes(),
        m : date.getSeconds(),
    }

    // 补位处理
    for(let name in dateArr){
        dateArr[name]=String(dateArr[name]).padStart(2,'0');
    }

    // 分隔符
    if(separator){
        sep=separator;
    }

    // 输出对应数据
    let str='';

    if(type.length>1){
        // 输出多项
        let typeArr=[];
        if(type=='all'){
            // 输出所有项
            for(let name in dateArr){
                typeArr.push(dateArr[name])
            }
        }else{
            // 输出所选项
            for(let i=0; i&lt;type.length; i++){
                for(let name in dateArr){
                    if(name==type[i]){
                        typeArr.push(dateArr[name])
                    }
                }
            }
        }
        str=typeArr.join(sep);
    }else{
        // 输出单项
        for(let name in dateArr){
            if(name==type){
                str=dateArr[name];
            }
        }
    }
    return str;
}

export default timeStamp;</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#cookieAdd">cookieAdd</a></li><li><a href="global.html#cookieGet">cookieGet</a></li><li><a href="global.html#haveCNChars">haveCNChars</a></li><li><a href="global.html#haveSpace">haveSpace</a></li><li><a href="global.html#isAndroid">isAndroid</a></li><li><a href="global.html#isApple">isApple</a></li><li><a href="global.html#isEmail">isEmail</a></li><li><a href="global.html#isIdCard">isIdCard</a></li><li><a href="global.html#isMail">isMail</a></li><li><a href="global.html#isMobile">isMobile</a></li><li><a href="global.html#isPhone">isPhone</a></li><li><a href="global.html#isQQ">isQQ</a></li><li><a href="global.html#isTel">isTel</a></li><li><a href="global.html#isTest">isTest</a></li><li><a href="global.html#isURL">isURL</a></li><li><a href="global.html#isWeixin">isWeixin</a></li><li><a href="global.html#lengthRange">lengthRange</a></li><li><a href="global.html#letterBegin">letterBegin</a></li><li><a href="global.html#localStorageAdd">localStorageAdd</a></li><li><a href="global.html#localStorageGet">localStorageGet</a></li><li><a href="global.html#pureNum">pureNum</a></li><li><a href="global.html#sessionStorageAdd">sessionStorageAdd</a></li><li><a href="global.html#sessionStorageGet">sessionStorageGet</a></li><li><a href="global.html#strDelRight">strDelRight</a></li><li><a href="global.html#strJoin">strJoin</a></li><li><a href="global.html#strTrim">strTrim</a></li><li><a href="global.html#timeStamp">timeStamp</a></li><li><a href="global.html#totalPage">totalPage</a></li><li><a href="global.html#urlParamAdd">urlParamAdd</a></li><li><a href="global.html#urlParamGet">urlParamGet</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Nov 22 2021 17:08:01 GMT+0800 (中国标准时间)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
